-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly Circular Linked List - Insert node to a specific position - based on user input.cpp
115 lines (108 loc) · 3.06 KB
/
Doubly Circular Linked List - Insert node to a specific position - based on user input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
using namespace std;
struct NODE {
int data;
NODE *prev;
NODE *next;
};
NODE *head, *tail, *prevnode, *temp;
void add(int value){
if(head == NULL){
head = tail = new NODE;
head->data = value;
head->prev = head->next = tail;
} else {
prevnode = tail;
tail->next = head->prev = new NODE;
tail = tail->next;
tail->prev = prevnode;
tail->data = value;
tail->next = head;
}
}
int addTo(int position, int value){
temp = head;
if(position >= 1){
for(int i = 1; i < position-1; i++){
temp = temp->next; // point to position immediate previous node
if(temp == head){
temp = 0;
break;
}
}
if(temp || position == 1){
NODE *newnode = new NODE;
newnode->data = value;
if(position == 1){
head->prev = newnode;
newnode->next = head;
head = newnode;
if(head->next == 0) tail = head;
tail->next = head;
head->prev = tail;
} else {
newnode->prev = temp;
newnode->next = temp->next;
temp->next = newnode;
if(newnode->next == head){
tail = head->prev = newnode;
tail->next = head;
} else {
newnode->next->prev = newnode;
}
}
}
}
if(position < 1 || temp == 0){
cout << "Data not inserted. Invalid position [" << position <<"]." << endl;
return false;
}
return true;
}
int print(NODE *ref){
if(ref == 0){
cout << "[EMPTY LIST]" << endl;
return false;
} else {
do {
cout << ref->data;
ref = ref->next;
if(ref != head) cout << " => ";
} while(ref != head);
cout << endl;
return true;
}
}
int main(){
cout << "Note! Enter zero (0) to exit from entering input. " << endl;
int input = 1, position = 0;
while(true){
if(head == 0){
cout << "Enter a value for the first/initial NODE: ";
cin >> input;
} else {
cout << "Enter a position to add a new NODE: ";
cin >> position;
cout << "Enter a value for the position [" << position << "]: ";
cin >> input;
}
if(input == 0){
cout << "Program exited!" << endl;
break;
} else {
bool added = true;
if(head == 0){
add(input);
} else {
added = addTo(position, input);
}
if(added){
position = (position) ? ++position : position;
cout << "Value [" << input << "] added at the position " << position << ". ";
cout << "Updated list: " << endl << "↳ ";
if(!print(head)) break;
}
cout << endl << endl;
}
}
}